package com.ruoyi.psi.sale.order.controller;


import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.psi.sale.order.BI.*;
import com.ruoyi.psi.sale.order.domain.*;
import com.ruoyi.psi.sale.order.service.ISaleOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

/**
 * 销售订单
 */
@RestController
@RequestMapping("/psi/sale/order")
@Api(value = "销售订单信息模块", tags = "销售订单接口")
public class SaleOrderController extends BaseController {
    @Autowired
    private ISaleOrderService saleOrderService;

    /**
     * 获取销售订单
     * @param cond
     * @return
     */
    @ApiOperationSupport(includeParameters = {"SaleOrderCond.saleOrderId", "SaleOrderCond.staffSaleId", "SaleOrderCond.staffCheckId", "SaleOrderCond.customerInfoId", "SaleOrderCond.createTime"})
    @ApiOperation(value = "获取销售订单列表",notes = "查询所有销售订单信息")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody SaleOrderCond cond) {
        startPage();
        List<SaleOrderDto> saleOrderDtos = saleOrderService.selectSaleOrderList(cond);
        return getDataTable(saleOrderDtos);
    }

    /**
     * 导出销售订单
     *
     * @param cond
     * @return
     */
    @PostMapping("/export")
    public AjaxResult export(@RequestBody SaleOrderCond cond) {
        List<SaleOrderDto> saleOrderDtos = saleOrderService.selectSaleOrderList(cond);
        ExcelUtil<SaleOrderDto> util = new ExcelUtil<>(SaleOrderDto.class);
        return util.exportExcel(saleOrderDtos, "销售订单单数据");
    }

    /**
     * 新增销售订单
     * @param saleOrder 销售订单单据
     * @return
     */
    @ApiOperation(value = "新增销售订单", notes = "新增销售订单信息")
    @ApiImplicitParam(name = "saleOrder", value = "新增销售订单", dataType = "SaleOrderDomain")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody SaleOrderDomain saleOrder) {
        try {
            saleOrderService.insertSaleOrder(saleOrder);
            return AjaxResult.success("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("新增失败");
        }
    }

    /**
     *
     * 更新销售订单
     * @param saleOrder 销售订单单据
     * @return
     */
    @ApiOperation(value = "修改销售订单", notes = "修改销售订单")
    @ApiImplicitParam(name = "saleOrder", value = "修改销售订单", dataType = "SaleOrder")
    @PostMapping("/edit")
    public AjaxResult edit(@Validated @RequestBody SaleOrderDomain saleOrder) {
        try {
            saleOrderService.updateSaleOrder(saleOrder);
            return AjaxResult.success("修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("修改失败");
        }
    }

    /**
     * 删除销售订单
     * 仅修改删除标志位
     *
     * @param saleOrder 销售订单单据
     * @return
     */
    @ApiOperation(value = "删除销售订单", notes = "删除销售订单,仅删除标志位")
    @ApiImplicitParam(name = "saleOrder", value = "删除销售订单", dataType = "SaleOrder")
    @PostMapping("/delete")
    public AjaxResult delete(@RequestBody SaleOrderDomain saleOrder) {

        return toAjax(saleOrderService.updateSaleOrderDeleteFlag(saleOrder));
    }

    /**
     * 查询订单金额排行信息
     * @return
     */
    @ApiOperation(value = "查询订单金额排行信息",notes = "查询订单金额排行信息")
    @PostMapping("/orderMoneyRank")
    public TableDataInfo selectSaleOrderRank(@RequestBody SaleOrderCond cond) {
        startPage();
        List<SaleOrderListDto> saleOrderListDtos = saleOrderService.selectSaleOrderListList(cond);
        return getDataTable(saleOrderListDtos);
    }

    /**
     * 获取业务员数据信息
     * @param cond
     * @return
     */
    @ApiOperation(value = "获取业务员数据列表",notes = "查询所有业务员数据信息")
    @PostMapping("/salesmanRank")
    public TableDataInfo selectSalesman(@RequestBody SaleOrderCond cond) {
        startPage();
        List<Salesman> salesmen = saleOrderService.selectSalesman(cond);
        return getDataTable(salesmen);
    }

    /**
     * 查询订单利润排行并分析
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询订单利润、成本",notes = "查询订单利润、成本")
    @PostMapping("/profit")
    public TableDataInfo selectSaleOrderProfit(@RequestBody SaleOrderCond cond) {
        startPage();
        List<SaleOrderProfit> saleOrderProfits = saleOrderService.selectSaleOrderProfit(cond);
        return getDataTable(saleOrderProfits);
    }

    /**
     * 查询订单成本
     * @param saleOrderId
     * @return
     */
    @ApiOperation(value = "分析称本来源",notes = "分析称本来源")
    @GetMapping("/cost/{saleOrderId}")
    public TableDataInfo selectSaleOrderCost(@PathVariable String saleOrderId) {
        startPage();
        List<SaleOrderCost> saleOrderCosts = saleOrderService.selectSaleOrderCost(saleOrderId);
        return getDataTable(saleOrderCosts);
    }

    /**
     * 查询月销售目标达成情况
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询月销售目标达成情况",notes = "查询月销售目标达成情况")
    @PostMapping("/saleTargetAchievement")
    public TableDataInfo selectSaleTargetAchievement(@RequestBody SaleOrderCond cond) {
        startPage();
        List<SaleTargetAchievement> saleTargetAchievements = saleOrderService.selectSaleTargetAchievement(cond);
        return getDataTable(saleTargetAchievements);
    }

    /**
     * 查询订单处理周期
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询订单处理周期",notes = "查询订单处理周期")
    @PostMapping("/orderProcessingCycle")
    public TableDataInfo selectOrderProcessingCycle(@RequestBody SaleOrderCond cond) {
        startPage();
        List<OrderProcessingCycle> orderProcessingCycles = saleOrderService.selectOrderProcessingCycle(cond);
        return getDataTable(orderProcessingCycles);
    }

    /**
     * 查询订单生产周期
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询订单生产周期",notes = "查询订单生产周期")
    @PostMapping("/orderProductionCycle")
    public TableDataInfo selectOrderProductionCycle(@RequestBody SaleOrderCond cond) {
        startPage();
        List<OrderProductionCycle> orderProductionCycleList = saleOrderService.selectOrderProductionCycle(cond);
        return getDataTable(orderProductionCycleList);
    }

    /**
     * 查询订单发货周期
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询订单发货周期",notes = "查询订单发货周期")
    @PostMapping("/orderDeliveryCycle")
    public TableDataInfo selectOrderDeliveryCycle(@RequestBody SaleOrderCond cond) {
        startPage();
        List<OrderDeliveryCycle> orderDeliveryCycleList = saleOrderService.selectOrderDeliveryCycle(cond);
        return getDataTable(orderDeliveryCycleList);
    }

    /**
     * 查询超期订单
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询超期订单",notes = "查询超期订单")
    @PostMapping("/overdueOrder")
    public TableDataInfo selectOverdueOrder(@RequestBody SaleOrderCond cond) {
        startPage();
        List<SaleOrderDto> orderDtoList = saleOrderService.selectOverdueOrder(cond);
        return getDataTable(orderDtoList);
    }

    /**
     * 查询产品订单数
     * 分月
     * @return
     */
    @ApiOperation(value = "查询产品订单数（分月）",notes = "查询产品订单数（分月）")
    @GetMapping("/selectProductOrderQuantity")
    public AjaxResult selectProductOrderQuantity() {
        List<SaleOrderProduct> saleOrderProductList = saleOrderService.selectProductOrderMonth();
        return AjaxResult.success("查询成功", saleOrderProductList);
    }

    /**
     * 查询产品订单数
     * 分月
     * @return
     */
//    @ApiOperation(value = "查询产品订单数（分月）2",notes = "查询产品订单数（分月）2")
//    @GetMapping("/selectProductOrderQuantity2")
//    public AjaxResult selectProductOrderQuantity2() {
//        List<SaleOrderProduct> saleOrderProductList = saleOrderService.selectProductOrderMonth();
//        List<SaleOrderProduct> saleOrderProductList1 = saleOrderService.selectProductOrderMonth2(saleOrderProductList);
//        return AjaxResult.success("查询成功", saleOrderProductList1);
//    }

    /**
     * 查询销售订单数量
     * 分月
     * @return
     */
    @ApiOperation(value = "查询销售订单数量（分月）",notes = "查询销售订单数量（分月）")
    @GetMapping("/saleOrderQuantity")
    public AjaxResult selectSaleOrderQuantity() {
        List<SaleOrderQuantity> saleOrderQuantities = saleOrderService.selectSaleOrderQuantity();
        return AjaxResult.success("查询成功", saleOrderQuantities);
    }

    /**
     * 查询客户订单
     * @param cond
     * @return
     */
    @ApiOperation(value = "查询客户订单",notes = "查询客户订单")
    @PostMapping("/customerOrder")
    public TableDataInfo selectCustomerOrder(@RequestBody SaleOrderCond cond) {
        startPage();
        List<CustomerOrder> customerOrderList = saleOrderService.selectCustomerOrder(cond);
        return getDataTable(customerOrderList);
    }

    /**
     * 通过销售订单查询销售订单进度
     * @return
     */
    @ApiOperation("通过销售订单查询销售订单进度")
    @GetMapping("/querySaleOrderProgress")
    public AjaxResult querySaleOrderProgress(@RequestParam String saleOrderId) {
        int res = saleOrderService.querySaleOrderProgress(saleOrderId);
        if (res <= 5 && res != 0) {
            return AjaxResult.success(res);
        }
        return AjaxResult.error("该销售订单暂无进度查询");
    }
}
